package cn.mdmbct.douyin.common.redis;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.util.Pool;

import java.util.concurrent.TimeUnit;

/**
 * Jedis操作Redis
 *
 * @author mdmbct  mdmbct@outlook.com
 * @date 2021/3/18 0:04
 * @modified mdmbct
 * @since 1.0
 */
@Slf4j
@RequiredArgsConstructor
public class JedisOps implements RedisOps {

    private final Pool<Jedis> jedisPool;

    @Override
    public <T> T getValue(String key, Class<T> valueClass) {

        Jedis jedis = null;
        T t = null;
        try {
            jedis = jedisPool.getResource();
            t = JSONObject.parseObject(jedis.get(key), valueClass);
        } catch (Exception e) {
            log.error("JedisOps#getValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return t;
    }

    @Override
    public String getValue(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            log.error("JedisOps#getValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    @Override
    public <T> void setValue(String key, T value, long expire, TimeUnit timeUnit) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            if (expire <= 0) {
                setValue(key, value);
            } else {
                jedis.psetex(key, timeUnit.toMillis(expire), JSONObject.toJSONString(value));
            }

        } catch (Exception e) {
            log.error("JedisOps#setValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    @Override
    public <T> void setValue(String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            jedis.set(key, JSONObject.toJSONString(value));

        } catch (Exception e) {
            log.error("JedisOps#setValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    @Override
    public Long getExpire(String key) {
        Jedis jedis = null;
        long expire = 0;
        try {
            jedis = jedisPool.getResource();
            expire = jedis.ttl(key);
        } catch (Exception e) {
            log.error("JedisOps#getValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return expire;
    }

    @Override
    public void setExpire(String key, long expire, TimeUnit timeUnit) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.pexpire(key, timeUnit.toMillis(expire));
        } catch (Exception e) {
            log.error("JedisOps#setValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    @Override
    public void delete(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e) {
            log.error("JedisOps#setValue " + e.getMessage());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
